/*
	Azid Engine is 3D Game Engine developed along side with Galactic Siege (see Google Code) 
	Copyright (C) 2010  Jorgen Ader

	This file is part of AzidEngine.

	AzidEngine is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	AzidEngine is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with AzidEngine.  If not, see <http://www.gnu.org/licenses/>. 
*/

#include "../includes/EngineSettingsManager.hpp"

using namespace Ogre;
using namespace std;
using namespace AzidEngine;

SettingsManager::SettingsManager( String filename ) : mFileName(filename)
{
	mChanged = false;
}

SettingsManager::~SettingsManager()
{
	mSettings.clear();
}

void SettingsManager::setFile( String fileName )
{
	mFileName = fileName;
}

String SettingsManager::getValue( Settings Setting )
{
	return mSettings[Setting];
}

void SettingsManager::setValue( Settings Setting, Ogre::String Value )
{
	mSettings[Setting] = Value;
	mChanged = true;
}

bool SettingsManager::Read()
{
	// TODO complete all changes
	fstream file;
	file.open(mFileName.c_str(), std::ios::in);
	if(file.is_open())
	{
		String buff1, buff2;
		while( getline(file, buff1, ' ') )
		{
			getline(file, buff2);
			if(buff1 != "" || buff2 != "")
			{
				if(buff1 == "screen_width")
					mSettings[SCREEN_WIDTH] = buff2;
				else if(buff1 == "screen_height")
					mSettings[SCREEN_HEIGHT] = buff2;
				else if(buff1 == "color_depth")
					mSettings[COLOR_DEPTH] = buff2;
				else if(buff1 == "rendering_system")
					mSettings[RENDERING_SYSTEM] = buff2;
				else if(buff1 == "fullscreen")
					mSettings[FULLSCREEN] = buff2;
				/* BLANK
				else if(buff1 == "")
				 	mSettings[] = buff2;
				 */
			}
		}
		file.close();
	}
	else
		return false;
	return true;
}

void SettingsManager::Write()
{
	if(Changed == true)
	{
		// TODO Change this to new format
		fstream file( mFileName.c_str(), std::ios::out);
		map<String, String>::iterator iter;
		for(iter = mSettings.begin(); iter != mSettings.end(); iter++)
		{
			if(iter->first == SCREEN_WIDTH)
				file << "screen_width " << iter->second << '\n';
			/* BLANK
			else if(iter->first == )
				file << " " << iter->second << '\n';
			 */
		}
		file.close();
		mChanged = false;
	}
}
